window.onload = function () {
	var oForm = document.forms[0];//表单
	var oSpan = document.querySelector("p .code");
	var oTit = document.querySelector(".users .tit");
	var unameFlag = false;//用户名
	var upwdFlag = false;//密码
	var uphoneFlag = false;//手机号
	var conpwdflag = false;
	var yzcodeflag = false;
	oForm.uname.oninput = function () {
		var reg = /^\w{4,20}$/i;
		// console.log(this.previousElementSibling||this.previousSibling);
		unameFlag = vlidaVal(this, reg, "用户名太短，请在4-20字符之间");
		let self = this;
		// if (unameFlag) {
		// 	var oUsers = JSON.parse($$$.getCookie("userlist") || "[]");
		// 	var strVal = this.value.trim();
		// 	unameFlag = !oUsers.some(function (el) {
		// 		return el.name == strVal;
		// 	})
		// 	if (unameFlag) {//到这一步是true 不存在
		// 		if (unameFlag) {
		// 			(this.previousElementSibling || this.previousSibling).innerText = "";
		// 		} else {
		// 			(this.previousElementSibling || this.previousSibling).innerText = "该用户已注册";
		// 		}
		// 	}
		// }
		// if(unameFlag){
		// 	$.ajax({
		// 		url:"./account/reg",
		// 		type:"post",
		// 		data:$("form").serialize()//前端发送到服务器的数据
		// 	}).then(function(response){//服务器响应到前端的数据response
		// 		// console.log(response);
		// 		// layer.msg("注册成功")
		// 		if (response.status != 200) {
		// 			(self.previousElementSibling || self.previousSibling).innerText = "该用户已注册";
		// 		} 
		// 	})
		// }
	}
	oForm.upwd.oninput = function () {
		var reg = /^\w{8,16}/i;
		upwdFlag = vlidaVal(this, reg, "密码长度必须是8-16个字符");
	}
	oForm.uphone.oninput = function () {
		var reg = /^[1][3-9]\d{9}$/;
		uphoneFlag = vlidaVal(this, reg, "手机号无效")
	}
	oForm.conpwd.oninput = function () {
		if (this.value == oForm.upwd.value) {
			(this.previousElementSibling || this.previousSibling).innerText = "";
			conpwdflag = true;
		} else {
			(this.previousElementSibling || this.previousSibling).innerText = "密码输入不一致";
			conpwdflag = false;
		}
	}
	oForm.ucode.oninput = function () {
		if (this.value.toUpperCase() != oSpan.innerText.toUpperCase()) {
			(this.previousElementSibling || this.previousSibling).style.background = "#FEE6E7";
			(this.previousElementSibling || this.previousSibling).innerText = "验证码输入有误";
			yzcodeflag = false;
		} else {
			(this.previousElementSibling || this.previousSibling).style.background = "#FFF";
			(this.previousElementSibling || this.previousSibling).innerText = "";
			yzcodeflag = true;
		}
	}
	oForm.agreen.onclick = function () {
		if (oForm.agreen.checked == false) {
			oForm.btn.style.background = "#AAAAAA";
			oTit.style.display = "block";
		} else {
			oForm.btn.style.background = "#43A41B";
			oTit.style.display = "none";
		}
	}
	function vlidaVal(el, reg, msg) {
		var flag = reg.test(el.value);
		if (flag) {
			(el.previousElementSibling || el.previousSibling).innerText = "";
		} else {
			(el.previousElementSibling || el.previousSibling).innerText = msg;
		}
		return flag;
	}
	oSpan.innerText = randomCode(4, 6);
	oSpan.onclick = function () {
		oSpan.innerText = randomCode(4, 6);
	}

	oForm.onsubmit = function () {
		if (unameFlag && upwdFlag && conpwdflag && uphoneFlag && yzcodeflag) {
			$.ajax({
				url:"./account/reg",
				type:"post",
				data:$("form").serialize()//前端发送到服务器的数据
			}).then(function(response){//服务器响应到前端的数据response
				// console.log(response);
				// layer.msg("注册成功")
				if (response.status == 200) {
					layer.msg("注册成功");
					// $("form").find("input").html("");
					window.location.reload();
				} else {
					layer.msg("注册失败")
				}
			})
		}
		return false;
	}
}